package com.luoyx.hauyne.security.util;

import com.luoyx.hauyne.security.pojo.CurrentSysUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 获取当前用户信息 工具类
 *
 * @author Alan.Luo
 */
public class SecurityUtils {

    private SecurityUtils() {

    }

    /**
     * 获取当前用户
     *
     * @return
     */
    public static CurrentSysUser getCurrentSysUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            return null;
        }
        Object principal = authentication.getPrincipal();

        return (CurrentSysUser) principal;
    }

    /**
     * 获取当前用户id
     *
     * @return
     */
    public static Long getCurrentSysUserId() {
        CurrentSysUser currentSysUser = getCurrentSysUser();
        if (null != currentSysUser) {
            return currentSysUser.getId();
        }

        return null;
    }
}

